#!/usr/bin/env ruby
# Contents: 
# Purpose:
# Status:
require  'socket'
require  'net/ftp'
require  'Library'
require 'ftools'

class Ftp_File_Manager
public
	# 1 is upload 0 is download

	def initialize()
		init_ftp()
	end 
	def ftp_configure(configurations)
		if configurations.length != 6 then 
			print "not enough information\n"
			return nil
		end
		config_file = File.open("FTP.config",File::CREAT|File::RDWR)
		configs = config_file.readlines
		i = 0 
		File.delete(config_file.path)
		config_file = File.open("FTP.config",File::CREAT|File::RDWR)
		while (i < 6)
			if configurations[i] != "------" then
				configs[i] = configurations[i].to_s
				configs[i] << "\n"
			end
			config_file.write(configs[i])
			i = i + 1
		end	
		@time_interval_update = configs[0].chomp("\n")
	        @time_of_last_update = configs[1].chomp("\n")
		@local_lib_loc = configs[2].chomp("\n")
		@remote_login_pw = configs[3].chomp("\n")
		@remote_login_name = configs[4].chomp("\n")
		@remote_loc = configs[5].chomp("\n")
		config_file.close	
	end
	def ftp_startup(flag) 
		ftp = connecting_ftp()
		if ftp == nil then 
			print "failure to connect to ftp\n"
		end
		ftp = login_ftp(ftp)
		if ftp == nil then 
			print "failure to login in to ftp\n"		
		end
		if flag == 1 then 						
			error = down_ftp(ftp)
			if error == nil then 
				print "failure to to download from ftp\n"		
			end
		elsif flag == 0 then
			error = up_ftp(ftp)
			if error == nil then 
				print "failure to to upload from ftp\n"		
			end
		end
		ftp.close
	return 1
	end
	def get_time
		@time_interval_update
	end
	def get_last_time
		@time_of_last_update
	end
	def set_last_time(t)
		a = Array.new(6)
		a[0] = "------"
		a[1] = "#{t}"
		a[2] = "------"
		a[3] = "------"
		a[4] = "------"
		a[5] = "------"
		ftp_configure(a)
	end
	def get_local_lib_loc
		@local_lib_loc
	end
	def get_remote_pw
		@remote_login_pw
	end
	def get_remote_login_name
		@remote_login_name
	end
	def get_remote_loc
		@remote_loc
	end
	def get_all
		ret = Array.new(6)
		ret[0] = @time_interval_update
		ret[1] = @time_of_last_update
		ret[2] = @local_lib_loc
		ret[3] = @remote_login_pw
		ret[4] = @remote_login_name
		ret[5] = @remote_loc
		return ret
	end
	# the will connect to the server returning the 
	#server connect
	def init_ftp()
		config_file = File.new("FTP.config",File::CREAT|File::RDWR,0777)
		configs = config_file.readlines
		if configs.length == 0 then
			File.truncate("FTP.config",0)
			i = 0
			while(i<6)
				config_file.write("------\n")
				i = i + 1
			end
			config_file.close
			return
		end

		@remote_loc = configs[5].chomp("\n")
		@remote_login_name = configs[4].chomp("\n")
		@remote_login_pw = configs[3].chomp("\n")
		@local_lib_loc = configs[2].chomp("\n")
		@time_of_last_update = configs[1].chomp("\n")
		@time_interval_update = configs[0].chomp("\n")
	end
	#connects to ftp
	def connecting_ftp()
		if @remote_loc == nil then
			print "no remote_loc set cannot connect\n"
			return nil
		end
		ftp =0 
			begin
				ftp = Net::FTP.new("#{@remote_loc}")
			rescue	Errno::ECONNREFUSED
					print "could not connect to the server"	
					return nil			
			end
		return ftp
	end
	# login into the ftp server
	def login_ftp(ftp)
		if @remote_login_name == nil or @remote_login_pw == nil then 
			print "cannot login into the ftp no user or password set"
			return nil 
		end
		log_ftp = ftp
		begin 
			log_ftp.login(@remote_login_name,@remote_login_pw)
		rescue Net::FTPPermError
			print "could not log into the server"
			return nil
		end
		return log_ftp
	end
	#downloads from ftp
	def down_ftp(ftp)
		if @remote_loc == nil then
			print "do not know what directory or file to grab or were to place"
			return nil
		end
		begin 
			file = ftp.gettextfile(@local_lib_loc,@local_lib_loc)	
		rescue Net::FTPPermError 
			puts "file does not exist or you dont have permissions to grab\n"
			return nil
		end
		return 1
	end
	#uploads from ftp
	def up_ftp(ftp)
		if @remote_loc == nil then
			puts "do not know what directory or file to grab or were to place"
			return nil
		end
		begin
			ftp.puttextfile(@local_lib_loc,@local_lib_loc)
		rescue  Net::FTPPermError
			puts "cannot put file onto ftp server\n"
			return nil
		end
		return 1
	end
end
